﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace BusinessLogicLayer
{
    public class NXBManager
    {
        public ActionResultAsList<NXB> LayDSNXB()
        {
            var ar = new ActionResultAsList<NXB>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    ar.ObjectList = db.NXBs.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResult ThemNXB(ref NXB nxb)
        {
            var ar = new ActionResult();
            using (var db = new ThuVienDbContext())
            {
                try
                {
                    db.NXBs.Add(nxb);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ar.AddException(ex);
                }
            }
            return ar;
        }
        public ActionResult XoaNXB(string manxb)
        {
            var ar = new ActionResult();
            using (var db = new ThuVienDbContext())
            {
                try
                {
                    var nxb = db.NXBs.Single(t => t.MaNXB == manxb);
                    db.NXBs.Remove(nxb);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ar.AddException(ex);
                }

            }
            return ar;
        }
        public ActionResult SuaNXB(NXB nxb)
        {
            var ar = new ActionResult();
            using (var db = new ThuVienDbContext())
            {
                try
                {
                    var t = db.NXBs.Single(s => s.MaNXB == nxb.MaNXB);
                    t.MaNXB = nxb.MaNXB;
                    t.TenNXB = nxb.TenNXB;
                    t.MoTa = nxb.MoTa;
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    ar.AddException(ex);
                }

            }
            return ar;
        }
        public ActionResultAsList<NXB> TimKiemNXBTheoMa(string manxb)
        {
            var ar = new ActionResultAsList<NXB>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var list = db.NXBs.Where(n => n.MaNXB == manxb);
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ActionResultAsList<NXB> TimKiemNXBTheoTen(string tennxb)
        {
            var ar = new ActionResultAsList<NXB>();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var list = db.NXBs.Where(n => n.TenNXB == tennxb);
                    ar.ObjectList = list.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }
        public ValidateResult KiemTraTrungMa(string manxb)
        {
            ValidateResult vr = new ValidateResult();
            try
            {
                using (var db = new ThuVienDbContext())
                {
                    var trungma = db.NXBs.Where(s => s.MaNXB == manxb);
                    if (trungma.Any())
                        vr.AddMessage("Trung ma nha xuat ban");
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                vr.AddMessage(ex.Message);
            }
            return vr;
        }        
        public ValidateResult KiemTraHopLe(NXB nxb)
        {
            ValidateResult vr = new ValidateResult();
            if (nxb.MaNXB == "")
                vr.AddMessage("Chưa nhập mã nxb!");
            if (nxb.TenNXB == "")
                vr.AddMessage("Chưa nhập tên nxb!");
            if (nxb.MoTa == "")
                vr.AddMessage("Chưa nhập mô tả nxb!");
            return vr;
        }
        public ValidateResult KiemTraHopLeNamXB(int namxb)
        {
            ValidateResult vr = new ValidateResult();
            DateTime d = new DateTime();
            d = DateTime.Now;
                if (namxb > d.Year)
                    vr.AddMessage("Năm XB không hợp lệ");
                return vr;
        }
    }
}
